package com.caiyouxi.notification.service.impl;

import com.caiyouxi.common.bean.QueryParams;
import com.caiyouxi.common.bean.ResData;
import com.caiyouxi.common.service.BaseService;
import com.caiyouxi.notification.mapper.NotificationMapper;
import com.caiyouxi.notification.model.NotificationBean;
import com.caiyouxi.notification.service.NotificationService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by Administrator on 2017/12/21.
 */
@Service
public class NotificationServiceImpl extends BaseService implements NotificationService{

    private Logger logger = Logger.getLogger(NotificationServiceImpl.class);

    @Autowired
    private NotificationMapper notificationMapper;


    /**
     *查询所有通知，放回通知列表
     * */
    @Override
    public ResData get_notification_list(NotificationBean notificationBean) {
        //获取分页大小
        if (StringUtils.isEmpty(notificationBean.getPagesize()) || "0".equals(notificationBean.getPagesize())) {
            notificationBean.setPagesize(10);
        }
        if (StringUtils.isEmpty(notificationBean.getPages()) || "0".equals(notificationBean.getPages())) {
            notificationBean.setPages(0);
        }
        QueryParams pageList = new QueryParams();
        List<NotificationBean> notifications = notificationMapper.get_notification_list(notificationBean);

        if (!StringUtils.isEmpty(notifications)) {
            for (NotificationBean up : notifications) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                if (!StringUtils.isEmpty(up.getCreate_time())) {
                    up.setCreate_date(format.format(up.getCreate_time()));
                }
            }
        }
        int total =  notificationMapper.get_total();
        logger.info("总共"+total+"记录");
        pageList.setList(notifications);
        pageList.setTotal(total);
        pageList.setPage_size(notificationBean.getPagesize());
        pageList.setPage(notificationBean.getPages());
        return page_list(pageList);
    }

    /**
     *新增通知
     * */
    @Override
    public ResData add_notification(NotificationBean notificationBean) {
        try {
            notificationMapper.add_notification(notificationBean);
        }catch (Exception e){
            return failed("faild","新增通知失败",null);
        }
        return success(null);
    }

    /**
     *删除通知
     * */
    @Override
    public ResData del_notification(NotificationBean notificationBean) {
        try {
            notificationMapper.del_notification(notificationBean);
        }catch (Exception e){
            return failed("faild","删除通知失败",null);
        }
        return success(null);
    }
}
